// 页面加载显示
$(function() {

	//获取服务类目
	ServiceCategory();
	
	//获取服务类目详情
	CategoryDetails(1);
	
	
	// 文本框字数限制
	$(".submit_information textarea").keyup(function(){
		var number = $(".submit_information textarea").val().length;
		$(".submit_information .number span").text(number);
	});
})

//获取服务类目
function ServiceCategory() {
	//获取 上一个搜索页面传来的参数 
	var searchUrl = window.location.href;
	var service_id = searchUrl.split("=")[1]; //截取 url中的“=”,获得“=”后面的参数
	$.ajax({
		type: 'GET',
		url: urlBox+'/api/website/ServiceCategory/',
		dataType: 'json',
		success: function(data) {
			var case_list = data.category_list;
			// console.log(data)
			for (var i = 0; i < case_list.length; i++) {
				var li = $('<li id="'+case_list[i].id+'" onclick="ServiceCategory_navigation(this)">'+case_list[i].service_name+'</li>')
				$(".navigation").append(li);
				$('.navigation>li').eq(service_id-1).addClass('highlight')
			}
		}
	})
}
// /服务品类 导航
function ServiceCategory_navigation(id) {
	$('.navigation>li').removeClass('highlight');
	$(id).addClass('highlight');
	// 获取服务类目详情
	var category_id= $(id).attr("id")
	CategoryDetails(category_id);
	$(".navigationBox").empty()
}

//获取服务类目详情
function CategoryDetails(category_id) {
	$.ajax({
		type: 'GET',
		url: urlBox+'/api/website/CategoryDetails/',
		data: {
			category_id: category_id,
		},
		dataType: 'json',
		success: function(data) {
			var case_list = data.details_list;
			// console.log(case_list.length)
			if(case_list.length !== 0){
				for (var i = 0; i < case_list.length; i++) {
					// console.log(case_list[i])
					var li = $('<li>'+
						'<div class="img">'+
							'<img src="'+case_list[i].main_img_url+'" alt="">'+
						'</div>'+
						'<div title="'+ case_list[i].name +'">'+case_list[i].name+'</div>'+
					'</li>')
					$(".navigationBox").append(li);
				}
				var h = $('.navigationBox li').height();
				var len = Math.ceil($('.navigationBox li').length/4);
				// console.log(h,len)
				$('.navigationBox').css({"height":h*len+80*len+'px'});
			}else{
				var li = $('<li class="available">'+
					'<img src="images/pc/pinlei/pinlei_shuju_icon.png" alt="">'+
				'</li>')
				$(".navigationBox").append(li);
				
				$('.navigationBox').css({"height":'350px'});
			}
			
		}
	})
}


// 提交信息
$('.submit').click(function(){
	var flag = false; //提示信息
	var message = '';
	var myreg = /^1[34578]\d{9}$/;
	var username = $(".submit_information .username").val();  //姓名
	var user_phone = $('.submit_information .user_phone').val(); //电话
	var profession = $(".submit_information .profession").val(); //职业
	var city_name1 = $('.submit_information #province select').eq(0).val(); //城市名称
	var city_name2 = $('.submit_information #province select').eq(1).val(); //省份名称
	var city_name3 = $('.submit_information #province select').eq(2).val(); //县区名称
	var remark = $(".submit_information textarea").val(); //备注
	
	
	console.log(username,user_phone,profession,city_name1,city_name2,city_name3)
	
	
	if (username == ''  || user_phone == '' || profession == '' || city_name3 == '请选择') {
		message = "姓名、手机、职业、城市不能为空！";

	} else if (user_phone.length != 11) {
		message = "请输入有效的手机号码！";
	} else if (!myreg.test(user_phone)) {
		message = "请输入有效的手机号码！";
	} else {
		flag = true;
		$.ajax({
			type: 'POST',
			url: urlBox+'/api/website/BusinessCooperation/',
			data: {
				username: username,//姓名
				user_phone: user_phone,//电话
				profession_id: profession,//职业
				city_name: city_name1,//城市名称
				province_name: city_name2,//省份名称
				region_name: city_name3,//县区名称
				remark: remark  //备注
			},
			dataType: 'json',
			success: function(data) {
				console.log(data)
				if (data.error == 'ok') {
					// 清空手机输入框
					$(".submit_information .username").val("");
					$('.submit_information .user_phone').val("");
					$(".submit_information .profession").val("");
					$('.submit_information .select').val("");
					$(".submit_information textarea").val("");
				}
			},
			error:function(data) {
				console.log(data)
			}
		});
	}
	if (!flag) {
		//提示错误效果
		var div = $('<div class="back"></div><div class="about_decorate_prompt"><div class="error">x</div><div>' + message +'</div><button>确定</button></div>');
		$('.submit_information .about_body').append(div);
		// 立即预约 显示提示消息
		prompt_fadeIn()
	} else {
		//提示正确效果
		var div = $(
			'<div class="back"></div><div class="about_decorate_prompt"><div class="sucs">✔</div><div class="successful">信息提交成功，客服人员24小时内和您联系。</div><button>确定</button></div>'
		);
		$('.submit_information .about_body').append(div);
	}
	// 点击确定隐藏提示信息
	if (flag == true) {
		// 立即预约 显示提示消息
		prompt_fadeIn();
		$('.submit_information .about_decorate_prompt button').click(function() {
			$('.submit_information .about_body .back').css({
				display: 'none'
			})
			// 立即预约 隐藏提示消息
			prompt_fadeOut();
		})
	} else {
		// 立即预约 显示提示消息
		prompt_fadeIn();
		$('.submit_information .about_decorate_prompt button').click(function() {
			$('.submit_information .about_body .back').css({
				display: 'none'
			})
			// 立即预约 隐藏提示消息
			prompt_fadeOut();
		})
	}
	
})



// 特色服务
$('.special_navigation>li').click(function() {
	$('.special_navigation>li').eq($(this).index()).addClass('highlight').siblings('.special_navigation>li').removeClass('highlight');
	$('.special_hide_box').eq($(this).index()).addClass('special_show_box').siblings('.special_hide_box').removeClass('special_show_box');
})



// 导航
$('.special_sourcenavigation>li').click(function() {
	
	$('.special_sourcenavigation>li').eq($(this).index()).addClass('highlight').siblings('.special_sourcenavigation>li').removeClass('highlight');
	
	$('.special_hide').eq($(this).index()).addClass('special_show').siblings('.special_hide').removeClass('special_show');
})

